package com.haople.sso.jdbc.authentication.impl;

import com.haople.sso.core.authentication.UserAuthorization;
import com.haople.sso.core.util.Md5;
import com.haople.sso.core.vo.User;
import com.haople.sso.jdbc.dao.UserDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;


public class DBUserAuthorizationImpl implements UserAuthorization {
    @Autowired
    private UserDao userDao;
    
    /*
     *  回去用户的key
     */
    public String authorization(String userName, String password) {
        String userKey=null;
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            return userKey;
        }
        User user=userDao.find(userName);
        if(user==null){
            return userKey;
        }
        if(Md5.md5(password).equals(user.getPassword())){ //如果密码相同
        	userKey=user.getUserId();
        }
        return userKey;
    }

	@Override
	public boolean exist(String userName) {
		if(userDao.find(userName)!=null) {
			return true;
		};
		return false;
	}

	@Override
	public boolean save(String userName, String password) {
		if(userDao.save(userName, Md5.md5(password))>0) {
			return true;
		};
		return false;
	}

	@Override
	public boolean changePassword(String userName, String newPassword) {
		if(userDao.updatePassword(userName, Md5.md5(newPassword))>0) {
			return true;
		};
		return false;
	}
	
	
}

